<template>
	<view class="content">
		<view class="card" v-for="(item,index) in dataList" :key="index">
			<view class="title-content">
				<view @click="toDetails(item)"
					style="display: flex;padding: 20rpx 20rpx 0 20rpx; justify-content: space-between;">
					<view class="title">
						{{item.orderNo}}
					</view>
					<view class="title-c" style="padding: 10rpx;border-radius: 8rpx;">
						{{cancelStatus[item.status] ? cancelStatus[item.status] : ''}}
					</view>
				</view>
				<view style="border-bottom: 1px solid #f6f6f6;margin: 20rpx;"></view>
				<view class="column" style="display: flex;justify-content: space-between;">
					<view class="font-weight-content">
						{{getObjectById(item.depotId).depotName}}
					</view>
					<view class="font-weight-content">
						共{{item.totalQuantity}}件
					</view>
				</view>
				<view style="display: flex;padding: 20rpx;justify-content: space-between;">
					<view class="title">
						{{item.createTime}} 下单
					</view>
					<view class="title-c" style="color: #201b8c;">
						提货码：{{item.deliveryCode}}
					</view>
				</view>
				<view style="border-bottom: 1px solid #f6f6f6;margin: 20rpx;"></view>
				<view class="content-footer" style="margin-top: 20rpx;padding-bottom: 10rpx;">
					<button v-if="item.status == 1" style="margin:10px ;line-height: 64rpx; font-weight: bold;"
						class="plain_bgColor" @click="refundOrder(item)">取消退库</button>
				</view>
			</view>
		</view>


		<u-empty v-if="!dataList.length" style="background-color: #fff;height: 100vh;" mode="data"
			icon="http://cdn.uviewui.com/uview/empty/data.png"></u-empty>
		<!-- 上拉加载更多 -->
		<uni-load-more v-else :status="loadMoreStatus"></uni-load-more>

		<!-- 生态窗组件 -->
		<u-modal :show="showCancelOrder" showCancelButton @confirm="cancelOrder" :content='showCancelOrderInfo'
			@cancel="() => showCancelOrder = false">
		</u-modal>
	</view>
</template>

<script>
	import {
		getCancelListAPi,
		cancelOrderAPi
	} from '@/api/cancelling.js';
import { setThemeColor } from '../../../utils/setTheme';
	export default {
		props: {
			tab: {
				type: [Object],
				default: ''
			},
			depotData: {
				type: [Array],
				default: ''
			}
		},
		data() {
			return {
				cancellForm: {
					current: 1,
					size: 10,
				},
				totalPage: 1,
				loadMoreStatus: 'noMore',
				dataList: [],
				cancelStatus: {
					1: '待出库',
					2: '已取出',
					3: '已取消'
				},
				showCancelOrder: false, //确认取消弹窗
				showCancelOrderInfo: '您确认取消该退库单吗？',
				ids: '', //取消退库id
				refreshing: false,
				refreshText: "",
				refreshFlag: false,
			}
		},
		onLoad() {
			this.bgColor = setThemeColor();
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.bgColor,
			});
		},
		created() {
			this.initData();
		},
		methods: {
			initData() {
				// 首次激活时被调用
				this.cancellForm.current = 1;
				this.loadMoreStatus = 'more';
				//需要初始化列表数据，根据数据类型
				this.cancellForm.status = this.tab.type;
				this.getCancelList();
			},
			// 触底事件 onReachBottom 加载更多数据
			scrolltolowerFn() {
				if (this.loadMoreStatus != 'more') {
					return;
				};
				this.cancellForm.current++;
				this.getCancelList(true);
			},
			toDetails(item) {
				this.$emit('toDetails', item)
			},
			// 搜索查询
			searchProduct(valueKey) {
				if(valueKey == ''){
					delete this.cancellForm.orderNo;
					this.cancellForm.current = 1;
					this.loadMoreStatus = 'more';
					this.getCancelList();
				}else{
					this.cancellForm.current = 1;
					this.cancellForm.orderNo = valueKey;
					this.loadMoreStatus = 'more';
					this.getCancelList();
				}
			},
			//时间查询
			calendarProduct(dates) {
				this.cancellForm.dateLimit = dates;
				this.loadMoreStatus = 'more';
				this.getCancelList()
			},
			//仓库筛选
			invoiceTypeConfirm(depotId) {
				this.cancellForm.depotId = depotId;
				this.loadMoreStatus = 'more';
				this.getCancelList()
			},
			onrefresh(e) {
				this.refreshing = true;
				this.refreshText = "刷新中...";
				setTimeout(() => {
					this.refreshing = false;
					this.refreshFlag = false;
					this.refreshText = "已刷新";
				}, 2000)
			},
			onpullingdown(e) {
				if (this.refreshing) {
					return;
				}
				this.pulling = false;
				if (Math.abs(e.pullingDistance) > Math.abs(e.viewHeight)) {
					this.refreshFlag = true;
					this.refreshText = "释放立即刷新";
				} else {
					this.refreshFlag = false;
					this.refreshText = "下拉可以刷新";
				}
			},
			// 回显仓库名称
			getObjectById(id) {
				return this.depotData[0].find(object => object.depotId === id);
			},
			// --------api------获取数据
			getCancelList(isMerge) {
				let that = this;
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				if (!isMerge) {
					this.cancellForm.current = 1;
					this.loadMoreStatus = 'more';
				};
				getCancelListAPi(this.cancellForm).then((res) => {
					let data = res.data;
					if (isMerge) {
						that.$util.SplitArray(data.records, that.dataList);
					} else {
						that.dataList = data.records;
					}
					that.totalPage = data.pages;
					if (data.pages == this.cancellForm.current || data.pages == 0) {
						that.loadMoreStatus = 'noMore';
					}
					uni.hideLoading();
				})
			},
			// 取消退库确认窗
			refundOrder(e) {
				this.showCancelOrder = true;
				this.ids = e.id;
			},
			// 确认取消退库操作
			cancelOrder() {
				cancelOrderAPi({
					ids: this.ids
				}).then((res) => {
					if (res.code == 200) {
						uni.showToast({
							title: '操作成功',
							icon: 'success',
							duration: 2000
						});
						setTimeout(() => {
							this.initData();
						},1000)
					}
				});
				this.showCancelOrder = false;
			}
		}
	}
</script>

<style lang="scss">
	.bg_color {
		@include main_bg_color(theme);
	}

	.plain_bgColor {
		@include main_color(theme);
		@include main_bg_rgba_color(theme, 0.05);
		border: 1rpx solid;
	}

	.content {
		// margin-top: 20rpx;
		padding-top: 20rpx;
	}

	.card {
		background-color: #fff;
		margin: 20rpx;
		border-radius: 10rpx;
	}

	.font-weight-content {
		font-weight: bold;
	}

	.column {
		padding: 20rpx;
		display: flex;
		flex-flow: row;
		text-align: center;
		justify-content: space-between;

		.title {
			display: flex;
			// flex: 1;
			width: 150rpx;
		}

		.title-c {
			display: flex;
			flex: 1;
			justify-content: left;
			// padding-left: 20rpx;
		}
	}

	.businessStock {
		display: flex;
		flex-flow: row;
		padding: 20rpx;

		.top_right {
			margin-left: 20rpx;
		}
	}
</style>